<?php

namespace App\Admin\Controllers;

use App\Models\WorkUserHistory;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;

class WorkUserHistoryController extends AdminController
{

    protected $title = '务工人员历史轨迹';
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new WorkUserHistory(), function (Grid $grid) {
            $grid->filter(function (Grid\Filter $filter) {
                $filter->panel();
                $filter->expand();
                $filter->like('id_card', '身份证号')->width(5);
                $filter->equal('type','类型')->select(
                    array(
                        '1'=> '入职',
                        '2' => '离职',
                        '3'=> '再入职',
                        '4'=> '劣迹'
                    )
                )->width(5);
                $filter->between('enter_time', '入职时间')->datetime()->width(5);
                $filter->between('exit_time', '离职时间')->datetime()->width(5);
            });
            $grid->disableCreateButton();
            $grid->disableViewButton();
//            $grid->disableEditButton();
            $grid->number('序号');
            $grid->column('name');
            $grid->column('id_card');
            $grid->column('engineering')->limit(10);
            $grid->column('type','类型')->display(function ($type) {
                if($type == 1){
                    return "<span style='color:red'>入职</span>";
                }
                if($type == 2){
                    return "<span style='color:red'>离职</span>";
                }
                if($type == 3){
                    return "<span style='color:red'>再入职</span>";
                }
                if($type == 4){
                    return "<span style='color:red'>劣迹</span>";
                }
            });


            $grid->column('details')->display(function ($details) {
                if($this->type  == 4){
                    if($details == 2){
                        return "<span style='color:red'>恶意讨薪</span>";
                    }
                    if($details == 3){
                        return "<span style='color:red'>打架斗殴</span>";
                    }
                    if($details == 4){
                        return "<span style='color:red'>严重违章</span>";
                    }
                    if($details == 5){
                        return "<span style='color:red'>其他行为</span>";
                    }
                }else{
                    return "<span style='color:red'>$details</span>";
                }
            });
            $grid->column('enter_time');
            $grid->column('exit_time');
            $grid->column('created_at');
//            $grid->column('updated_at')->sortable();

        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new WorkUserHistory(), function (Show $show) {
            $show->field('id');
            $show->field('name');
            $show->field('id_card');
            $show->field('engineering');
            $show->field('type');
            $show->field('details');
            $show->field('enter_time');
            $show->field('exit_time');
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new WorkUserHistory(), function (Form $form) {
            $form->display('id');
//            $form->text('name');
//            $form->text('id_card');
//            $form->text('engineering');
//            $form->text('type');
//            $form->text('details');
//            $form->text('enter_time');
            $form->datetime('enter_time','入职时间')->format('YYYY-MM-DD')->required();
            $form->datetime('exit_time','离职时间')->format('YYYY-MM-DD')->required();
//            $form->text('exit_time');

            $form->display('created_at');
            $form->display('updated_at');
        });
    }
}
